package com.td.module.system.controller.admin.video;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.system.controller.admin.video.vo.video.*;
import com.td.module.system.convert.video.VideoConvert;
import com.td.module.system.dal.dataobject.video.VideoDO;
import com.td.module.system.service.video.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 萤石账号管理")
@RestController
@RequestMapping("/system/video")
@Validated
public class VideoController {

    @Resource
    private VideoService videoService;

    @PostMapping("/create")
    @ApiOperation("创建萤石账号管理")
    public CommonResult<Long> createVideo(@Valid @RequestBody VideoCreateReqVO createReqVO) {
        return success(videoService.createVideo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新萤石账号管理")
    public CommonResult<Boolean> updateVideo(@Valid @RequestBody VideoUpdateReqVO updateReqVO) {
        videoService.updateVideo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除萤石账号管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteVideo(@RequestParam("id") Long id) {
        videoService.deleteVideo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得萤石账号管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<VideoRespVO> getVideo(@RequestParam("id") Long id) {
        VideoDO video = videoService.getVideo(id);
        return success(VideoConvert.INSTANCE.convert(video));
    }

    @GetMapping("/getToken")
    @ApiOperation("获得萤石账号管理")
    @ApiImplicitParam(name = "appKey", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<VideoRespVO> getToken(@RequestParam("appKey") String appKey) {
        VideoDO video = videoService.getToken(appKey);
        return success(VideoConvert.INSTANCE.convert(video));
    }

    @GetMapping("/list")
    @ApiOperation("获得萤石账号管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<VideoRespVO>> getVideoList(@RequestParam("ids") Collection<Long> ids) {
        List<VideoDO> list = videoService.getVideoList(ids);
        return success(VideoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得萤石账号管理分页")
    public CommonResult<PageResult<VideoRespVO>> getVideoPage(@Valid VideoPageReqVO pageVO) {
        PageResult<VideoDO> pageResult = videoService.getVideoPage(pageVO);
        return success(VideoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出萤石账号管理 Excel")
    @OperateLog(type = EXPORT)
    public void exportVideoExcel(@Valid VideoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VideoDO> list = videoService.getVideoList(exportReqVO);
        // 导出 Excel
        List<VideoExcelVO> datas = VideoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "萤石账号管理.xls", "数据", VideoExcelVO.class, datas);
    }

}
